//: Playground - noun: a place where people can play

import UIKit

let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein
let dollarSign = "x24" // $, Unicode scalar U+0024
let blackHeart = "u2665" // ♥, Unicode scalar U+2665
let sparklingHeart = "U0001F496" // , Unicode scalar U+1F496

//初始化一个空串
var emptyStr = ""
var anotherEmptyStr = String()
var emptyStrNil:String!
if emptyStr.isEmpty{
    print("Nothing")
}

emptyStrNil = "123"

let intNum = Int(emptyStrNil)

if emptyStr == anotherEmptyStr{
    
    print("Not nil")
}else{
    
    print("nil")
}

//变长字符串

var variableString = "Horse"
variableString += " and carriage"

//字符串不是指针，而是实际的值
//在Swift中，一个String类型就是一个实际的值，当定义一个新的String，并且将之前的String值拷贝过来的时候，是实际创建了一个相等的新值，而不是仅仅像指针那样指向过去

//遍历字符窜
for i in variableString.characters {
    
    print(i)
}

//获取字符串的长度
let unusualMenagerie = "Koala , Snail , Penguin , Dromedary "
print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")

//字符串的比较

let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lot alike, you and I."
if quotation == sameQuotation {
    print("These two strings are considered equal")
}

//前缀（prefix）相等和后缀(hasSuffix)相等
let romeoAndJuliet = [
    "Act 1 Scene 1: Verona, A public place",
    "Act 1 Scene 2: Capulet's mansion",
    "Act 1 Scene 3: A room in Capulet's mansion",
    "Act 1 Scene 4: A street outside Capulet's mansion",
    "Act 1 Scene 5: The Great Hall in Capulet's mansion",
    "Act 2 Scene 1: Outside Capulet's mansion",
    "Act 2 Scene 2: Capulet's orchard",
    "Act 2 Scene 3: Outside Friar Lawrence's cell",
    "Act 2 Scene 4: A street in Verona",
    "Act 2 Scene 5: Capulet's mansion",
    "Act 2 Scene 6: Friar Lawrence's cell"
]
//前缀相等
var act1SceneCount = 0
for scene in romeoAndJuliet {
    if scene.hasPrefix("Act 1 ") {
        ++act1SceneCount
    }
}
print("There are \(act1SceneCount) scenes in Act 1")

//后缀相等
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
    if scene.hasSuffix("Capulet's mansion") {
        ++mansionCount
    } else if scene.hasSuffix("Friar Lawrence's cell") {
        ++cellCount
    }
}
print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")

